// import { addbodyluck } from "../全局脚本/增加隐藏幸运值"
// import * as UpgradeWeapon from "../全局脚本/沙巴克武器升级设置"



//人物死亡触发
GameLib.onPlayerDie = (Player: TPlayObject, Killer: TActor): void => {
    if (Killer) {
        let str = `玩家[${Player.Name}]在【${Player.Map.Name}】被${Killer.IsPlayer() ? "玩家" : "凶狠的怪物"}[${Killer.Name.replace(/[0-9]+/g, "")}]杀害了`
        GameLib.Broadcast(str)
        saveLog(str, LogType.Die, Player)
    }
    else {
        let str = `玩家[${Player.Name}]在【${Player.Map.Name}】死亡`
        GameLib.Broadcast(str)
        saveLog(str, LogType.Die, Player)
    }

}


//杀人触发
GameLib.onKillPlayer = (Killer: TPlayObject, Player: TPlayObject): void => {
    // addbodyluck(Player, -500)
}


//人物升级触发
GameLib.onPlayerLevelUp = (Player: TPlayObject, Level: number): void => {
    // addbodyluck(Player, 100)
}



//人物攻击时触发
GameLib.onPlayerAttack = (Player: TPlayObject, UserMagic: TUserMagic, Target: TActor, Accept: boolean): boolean => {

    //检测是否升级武器
    // if (Player.Weapon != undefined) {
    //     if (Player.Weapon.DisplayName.includes('(*)') && Player.PoseCreate != null) {
    //         UpgradeWeapon.WeaponTest(Player)
    //     }
    // }

    return true

}


//人物获得经验值时触发
// GameLib.onGetExp = (Player: TPlayObject, Exp: number, ResultExp: number): number => {

//     addbodyluck(Player, ResultExp * 0.002)
//     return ResultExp
// }



import * as 可视仓库 from "../附加功能/可视仓库"
import * as 交易中心 from "../附加功能/交易中心"
import * as 回收 from "../附加功能/回收"
import * as 商城 from "../附加功能/商城"
import * as 传送 from "../附加功能/传送"
import * as 功能大全 from "../附加功能/功能大全"
import * as 测试 from "../测试/测试"
import { getSkillName, getCustomSkillManage, getDamageChangeManage, getFreshBoss, getFreshMon, getItemMangae, getSkillManage, getSlaveManage, getSpecialMonManage, getMonItemManage } from "./GlobalObjectManage"
import { MonitorRevival } from "./QManage"
import { changeCustomAbility } from "../附加功能/鉴定"
import { Second } from "../配置/基础配置"
import { 开始挂机, 结束挂机 } from "../附加功能/挂机"
import { LogType, saveLog } from "../utils/LogUtil"

//点击背包物品触发 
GameLib.onUIActivedBagItemEvent = (Player: TPlayObject, UIName: string, ClientItemBagIndex: number, Item: TUserItem, KeyCtrl: boolean, KeyAlt: boolean, MouseButton: TMouseButton): void => {
    if (UIName == '分页仓库') {
        可视仓库.StoreItems(Player, Item)
    }
}

GameLib.onSideBarButtonClick = (Player: TPlayObject, AName: string): void => {
    if (Player.Death) {
        return
    }

    let Args = null;
    switch (AName) {
        case '回收':
            回收.Main(GameLib.QFunctionNpc, Player, Args)
            break
        case '商城':
            商城.Main(GameLib.QFunctionNpc, Player, Args)
            break
        case '传送':
            传送.Main(GameLib.QFunctionNpc, Player, Args)
            break
        case '游戏功能':
            Args = new TArgs()
            Args.Add(1)
            可视仓库.ViewItems(GameLib.QFunctionNpc, Player, Args)
            break
        case '邮件':
            Player.OpenMailBox();
            break
        case '交易中心':
            Args = new TArgs()
            交易中心.Main(GameLib.QFunctionNpc, Player, Args);
            break
        case '功能大全':
            功能大全.Main(GameLib.QFunctionNpc, Player, null);
            break
        case '可视仓库':
            可视仓库.Main(GameLib.QFunctionNpc, Player, null);
            break
        case '测试':
            测试.Main(GameLib.QFunctionNpc, Player, null);
            break
    }

}

// 热键触发
GameLib.onSendHotKey = (Player: TPlayObject, Akey: number, KeyCtrl: boolean, KeyAlt: boolean, ATargetActor: TActor, X: number, Y: number): void => {

}

//怪物攻击前触发
GameLib.onMonSelectMagicBeforeAttack = (AMon: TActor, ATarget: TActor, AMagicID: number): number => {


    return getSpecialMonManage().onMonSelectMagic(AMon, ATarget, AMagicID);
}
GameLib.onAINpcExecute = (Actor: TActor) => {
    return getSpecialMonManage().onAINpc(Actor)
}

//怪物死亡触发
GameLib.onMonitorDie = (Envir: TEnvirnoment, ActorObject: TActor, Killer: TActor, Tag: number): void => {

    getSpecialMonManage().onMonDie(Envir, ActorObject, Killer, Tag);

    // getFreshBoss().onBossDie(Envir, ActorObject, Killer, Tag);

    // console.log(`怪物${ActorObject.Name}被${Killer ? Killer.Name : 'XXX'}杀死  标记${Tag}`)



    if (Killer) {
        let name = Killer.Name;
        if (Killer.Master) {
            name = Killer.Master.Name;
        }
        saveLog(`${name}杀死了${ActorObject.Name}`, LogType.Die)


    }

    getMonItemManage().onMonitorDie(Envir, ActorObject, Killer, Tag);


}

//受伤触发
GameLib.onMonitorDamageEx = (ActorObject: TActor, ADamageSource: TActor, Tag: number, SkillID: number, SkillLevel: number, Value: number): number => {

    // console.log("onMonitorDamageEx:" + SkillID)
    // console.log(ADamageSource.Name + "  zero:" + ActorObject.Name + "  " + Value)

    Value = getDamageChangeManage().onMonitorDamageEx(ActorObject, ADamageSource, Tag, SkillID, SkillLevel, Value);

    getFreshBoss().onMonDamage(ActorObject, ADamageSource, Value);


    return Value
}

// GameLib.onAttackActorDamageIsZero = (Attacker: TActor, Target: TActor, Reason: number, Damage: number): number => {
//     //用于拦截当攻击者的攻击力 小于 被攻击者的防御, 这时候攻击会全是0 的问题
//     //以下修改 会造成最低伤害 为 1 （Reason伤害为0原因 目前只有0 表示目标的物理防御大于攻击者的最大攻击）
//     console.log(Attacker.Name + "  zero:" + Target.Name + "  " + Damage)
//     return 0
// }

//玩家释放魔法触发  返回是否禁止此法术  true为不释放此木法,  false为释放
GameLib.onMagicSpell = (Player: TPlayObject, UserMagic: TUserMagic, nTargetX: number, nTargetY: number,
    Target: TActor, Handled: boolean): boolean => {
    return getSkillManage().onMagicSpell(Player, UserMagic, nTargetX, nTargetY, Target, Handled);
};


// 自定义技能触发脚本执行
// ATarget 这个参数可能是攻击对象，也可能是施法者自身
// 注意:当技能魔法对象为自身时，ATargetX,ATargetY的坐标为鼠标指向的坐标
//老的
GameLib.onMagicNpcExecute = (AMethod: string, ASource: TActor, ATarget: TActor,
    ATargetX: number, ATargetY: number, AMouseX: number, AMouseY: number,
    AList: TActorList, AMagic: TUserMagic, Processed: boolean): boolean => {

    return true;
};
// 独立自定义技能执行脚本触发
GameLib.onSkillActionExecute = (Source: TActor, Target: TActor, AMethod: string, nParam1: number, nParam2:
    number, sParam1: string, sParam2: string): void => {
    getCustomSkillManage().onSkillExecute(Source, Target, AMethod, nParam1, nParam2, sParam1, sParam2);
};


// 玩家进入地图前触发:Accept表示是否允许进入,默认为True
GameLib.onCheckEnterMap = (Play: TPlayObject, SourceEnvir: TEnvirnoment, DestEnvir: TEnvirnoment,
    SourceX: number, SourceY: number, DestX: number, DestY: number, Accept: boolean): boolean => {

    return true;
};

// 玩家进入地图后触发
GameLib.onPlayerEnterMap = (Play: TPlayObject, SourceEnvir: TEnvirnoment, DestEnvir: TEnvirnoment, SourceX: number,
    SourceY: number, DestX: number, DestY: number): void => {
    getFreshMon().HumanLeave(SourceEnvir);
    getFreshMon().HumanEnter(DestEnvir);
    saveLog(`${Play.Name}从地图${SourceEnvir.Name}进入地图${DestEnvir.Name}`, LogType.Map, Play)
};

// 玩家登录触发
// GameLib.onPlayerLogin =(PlayObject : TPlayObject, OnlineAddExp : boolean):void =>{
//     console.log(`${PlayObject.Name}在地图${PlayObject.GetMapName()}上线`)
// };

// 人物小退触发  OnlineAddExp:是否离线挂机
GameLib.onPlayerReconnection = (PlayObject: TPlayObject, OnlineAddExp: boolean): void => {
    getFreshMon().HumanLeave(PlayObject.Map);
    getSlaveManage().saveSlave(PlayObject);
    saveLog(`${PlayObject.Name}在地图${PlayObject.Map.Name}小退`, LogType.Map, PlayObject)
};

// 人物离线触发 OnlineAddExp:是否离线挂机
GameLib.onPlayerOffLine = (PlayObject: TPlayObject, OnlineAddExp: boolean): void => {
    getFreshMon().HumanLeave(PlayObject.Map);
    getSlaveManage().saveSlave(PlayObject);
    saveLog(`${PlayObject.Name}在地图${PlayObject.Map.Name}大退`, LogType.Map, PlayObject)
};


// 当对玩家或者怪物添加Buff时触发
GameLib.onAddBuff = (Actor: TActor, Buff: TBuff, Accept: boolean): boolean => {
    // console.log(`${Actor.Name}添加了BUFF${Buff.GroupID}类型为${Buff.GetBuffAbilityType()}值${Buff.BuffValue}`)
    return true;
};

// 移除Buff时触发
GameLib.onRemoveBuff = (Actor: TActor, Buff: TBuff): void => {
    getSpecialMonManage().onRemoveBuff(Actor, Buff);
    // console.log(`${Actor.Name}移除了BUFF${Buff.GroupID}类型为${Buff.GetBuffAbilityType()}值${Buff.BuffValue}`)
};


// 怪物复活(刷新)时触发: Envir地图环境,Actor怪物信息,Tag为怪物标志
GameLib.onMonitorRevival = (Envir: TEnvirnoment, ActorObject: TActor, Tag: number): void => {
    MonitorRevival(Envir, ActorObject, Tag)
    getSpecialMonManage().onMonFresh(Envir, ActorObject);
};

// 从地上拾取物品时触发
// GameLib.onPickupItem = (Player : TPlayObject,Envir : TEnvirnoment,UserItem : TUserItem,Accept : boolean):boolean =>{
//     getItemMangae().onPickupItem(Player,Envir,UserItem,Accept)
//     return true
// };


// 背包事件触发
// Item:和当前事件相关的物品; EventType: 事件类型{0:增加到背包,1:从背包中移出}; EventID：事件ID
// EventType:0,
// EventID:
// 0:捡取
// 1:怪物身上挖出
// 2:挖矿挖出
// 3:解包
// 4:从身上取下
// 5:商店购买
// 6:元宝商城购买
// 7:礼金商城购买
// 8:玩家交易获得
// 9:交易市场购买
// 10:交易市场下架
// 11:命令制造获得
// 12:取仓库
// 13:其他

// EventType:1,
// EventID:
// 0:丢弃
// 1:死亡爆出
// 2:出售
// 3:正常使用
// 4:穿戴到身上
// 5:交易给其他玩家
// 6:到期销毁
// 7:破损销毁
// 8:上架到交易市场
// 9:用脚本执行销毁或拿走
// 10:存
// 11:其他
GameLib.onBagItemEvent = (Player: TPlayObject, Item: TUserItem, EventType: number, EventID: number): void => {
    if (EventType == 0) {
        if (EventID == 0 || EventID == 1) {
            getItemMangae().onPickupItem(Player, Item)
        }
    }
};

// 宝宝杀怪触发
GameLib.onSlaveKillMonster = (Player: TPlayObject, Slave: TActor, Monster: TActor): void => {
    getSlaveManage().onSlaveKillMonster(Player, Slave, Monster);
};
// 宝宝升级触发 Master:BB属于谁(可能是人形怪) Slave:当前升级的BB NewLevel:将要升到的级数 Accept:是否允许升级,默认为true,如果设置为false则BB不可升级
GameLib.onSlaveLevelUp = (Master: TActor, Slave: TActor, NewLevel: number, Accept: boolean): boolean => {
    return false;
};

// 穿戴装备前触发
GameLib.onTakeOnItem = (Player: TPlayObject, UserItem: TUserItem, ItemWhere: number, Accept: boolean): boolean => {
    if (ItemWhere === TItemWhere.wWeapon || ItemWhere === TItemWhere.wDress) {
        let lastTime = Player.R.lastTakeOffTime || 0
        let cha = GameLib.TickCount - lastTime;
        if (cha < 10 * Second) {
            Player.SendMessage('10秒之内不允许重复穿上武器和衣服', 0);
            return false;
        }

        Player.R.lastTakeOffTime = GameLib.TickCount
    }
    return true
};

// 穿戴装备且属性变化后触发
GameLib.onAfterTakeOnItem = (Player: TPlayObject, TakeOnUserItem: TUserItem, TakeOffItem: TUserItem, ItemWhere: number): void => {
    changeCustomAbility(Player)
};


// 脱装备前触发
GameLib.onTakeOffItem = (Player: TPlayObject, UserItem: TUserItem, ItemWhere: number, Accept: boolean): boolean => {
    // console.log(`${Player.Name}脱下装备${UserItem.Name} ${GameLib.TickCount}`)


    return true
};
// 脱装备且属性变化后触发
GameLib.onAfterTakeOffItem = (Player: TPlayObject, TakeOffItem: TUserItem, ItemWhere: number): void => {
    changeCustomAbility(Player)
};


// 内挂点击开始挂机触发
GameLib.onStartAutoFight = (Player: TPlayObject): void => {
    开始挂机(Player)
};
// 内挂点击开始挂机触发
GameLib.onStopAutoFight = (Player: TPlayObject): void => {
    结束挂机(Player)
};